package yxy.cherry.data;

import java.io.File;
import java.io.FileOutputStream;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import yxy.apple.util.FileUtility;
import yxy.apple.util.JSONUtility;
import yxy.apple.util.StringUtillity;

public class DataMakerGameString {

	static String packageName = "yxy.cherry.data.string";

	public void make() {
		try {
			File file = new File(System.getProperty("user.dir") + "/data/json/gamestring.json");
			if (!file.exists()) {
				return;
			}

			makeFile(file, DataMaker.DIR, packageName);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	void makeFile(File file, String dir, String packageName) throws Exception {
		String path = dir + "/" + packageName.replaceAll("\\.", "/");
		String filename = "GameString";// 文件名

		File output = new File(path + "/" + filename + ".java");
		if (output.exists()) {
			output.delete();
		}
		FileUtility.create(output);

		System.err.println("\r\nmakefile::" + filename);
		String content = assembleFile(packageName, filename, file);
		FileOutputStream fos = new FileOutputStream(output);
		fos.write(content.getBytes("UTF-8"));
		fos.flush();
		fos.close();

		System.err.println(output.getName() + "文件生成");
	}

	private String assembleFile(String packageName, String filename, File file) throws Exception {
		StringBuffer buffer = new StringBuffer();

		// 文件头
		buffer.append("package yxy.cherry.data.string;\r\n" + //
				"\r\n" + //
				"import yxy.cherry.data.DataCenter;\r\n" + //
				"import yxy.cherry.data.bean.gamestring;\r\n" + //
				"\r\n" + //
				"public class GameString {\r\n" + //
				"\r\n"); //

		JSONArray dataArray = JSONUtility.readArray(file);
		dataArray.forEach(jsonObject -> assembleJsonObject((JSONObject) jsonObject, buffer));

		// 文件尾
		buffer.append("	static private GameString format(int id, Object... args) {\r\n" + //
				"		gamestring _gamestring = DataCenter.getData(id, gamestring.class);\r\n" + //
				"		String text = _gamestring.getCn();\r\n" + //
				"		if (args.length > 0) {\r\n" + //
				"			text = String.format(text, args);\r\n" + //
				"		}\r\n" + //
				"		GameString string = new GameString();\r\n" + //
				"		string.text = text;\r\n" + //
				"		string.chattype = _gamestring.getChattype();\r\n" + //
				"		return string;\r\n" + //
				"	}\r\n" + //
				"\r\n" + //
				"	// bean==========================================================================\r\n" + //
				"\r\n" + //
				"	private String text;\r\n" + //
				"	private int chattype;\r\n" + //
				"\r\n" + //
				"	public String getText() {\r\n" + //
				"		return text;\r\n" + //
				"	}\r\n" + //
				"\r\n" + //
				"	public int getChattype() {\r\n" + //
				"		return chattype;\r\n" + //
				"	}\r\n" + //
				"}\r\n"); //

//		System.err.println(buffer.toString());

		return buffer.toString();
	}

	private StringBuffer assembleJsonObject(JSONObject jsonObject, StringBuffer buffer) {
		String key = jsonObject.getString("key");
		String text = jsonObject.getString("cn");
		int id = jsonObject.getIntValue("id");
		JSONArray params = jsonObject.getJSONArray("beizhu");
		text = text.replaceAll("%d", "%s");
		int count = StringUtillity.count(text, "%s");

		buffer.append("	static public GameString " + key + "(");
		for (int i = 0; i < count; i++) {
			if (i < params.size()) {
				buffer.append("Object " + params.getString(i));
			} else {
				buffer.append("Object arg" + i);
			}
			if (i < count - 1) {
				buffer.append(", ");
			}
		}
		buffer.append(") {\r\n" + //
				"		return format(" + id); //
		for (int i = 0; i < count; i++) {
			if (i < params.size()) {
				buffer.append(", " + params.getString(i));
			} else {
				buffer.append(", arg" + i);
			}
		}
		buffer.append(");\r\n" + //
				"	}\r\n" + //
				"\r\n" + //
				""); //

		return buffer;
	}

}
